﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KasaTakip
{
    public partial class frmCariTanitim : Form
    {
        List<Magaza> _magazalar = new List<Magaza>();

        public frmCariTanitim()
        {
            InitializeComponent();
        }

        private void frmCariTanitim_Load(object sender, EventArgs e)
        {
            Listele();
            cmbDepo.Items.AddRange(_magazalar.Select(x => x.MagazaAdi).ToArray());
        }

        private void Listele()
        {
            using (KasaEntity ent = new KasaEntity())
            {
                gridControl1.DataSource = (from c in ent.Cariler
                                           join d in ent.Magazalar on c.MagazaNo equals d.MagazaNo
                                           select new
                                           {
                                               Magaza = d.MagazaAdi,
                                               CariKodu = c.Kodu,
                                               CariAdi = c.Unvani
                                           }).ToList();

                _magazalar.Clear();
                _magazalar.AddRange(ent.Magazalar.ToArray());
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (KasaEntity ent = new KasaEntity())
            {
                Cari cr = null;
                if (gridView1.FocusedRowHandle > -1)
                {
                    string kodu = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["CariKodu"]);
                    cr = ent.Cariler.Where(x => x.Kodu == kodu).FirstOrDefault();
                    cr.MagazaNo = _magazalar.Where(x => x.MagazaAdi == cmbDepo.Text).Select(x => x.MagazaNo).FirstOrDefault();
                    cr.Unvani = txtAdi.Text;
                }
                else
                {
                    cr = new Cari();
                    cr.Kodu = txtKodu.Text;
                    cr.MagazaNo = _magazalar.Where(x => x.MagazaAdi == cmbDepo.Text).Select(x => x.MagazaNo).FirstOrDefault();
                    cr.Unvani = txtAdi.Text;
                    ent.AddToCariler(cr);
                }

                ent.SaveChanges();
            }

            Listele();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (KasaEntity ent = new KasaEntity())
            {
                Cari cr = null;
                if (gridView1.FocusedRowHandle > -1)
                {
                    string kodu = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["CariKodu"]);
                    cr = ent.Cariler.Where(x => x.Kodu == kodu).FirstOrDefault();
                    ent.DeleteObject(cr);
                }
                ent.SaveChanges();
            }

            Listele();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                txtKodu.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["CariKodu"]);
                txtAdi.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["CariAdi"]);
                cmbDepo.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["Magaza"]);
            }
        }
    }
}
